package com.chainshop.product.controller;

import com.chainshop.common.annotation.Log;
import com.chainshop.common.core.controller.BaseController;
import com.chainshop.common.core.domain.AjaxResult;
import com.chainshop.common.core.page.TableDataInfo;
import com.chainshop.common.enums.BusinessType;
import com.chainshop.common.utils.poi.ExcelUtil;
import com.chainshop.product.domain.ItemCategory;
import com.chainshop.product.mapper.ProductCategoryMapper;
import com.chainshop.product.service.IItemCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 存储商品类别的Controller
 * 
 * @author hmy
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/product/category")
public class ItemCategoryController extends BaseController
{
    @Autowired
    private IItemCategoryService itemCategoryService;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    //查询商品分类信息
    @GetMapping("/getProductCategory")
    public AjaxResult getProductCategory()
    {
        List<ItemCategory> itemCategories = productCategoryMapper.selectAll();
        return success(itemCategories);
    }


    /**
     * 查询存储商品类别的列表
     */
    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemCategory itemCategory)
    {
        startPage();
        List<ItemCategory> list = itemCategoryService.selectItemCategoryList(itemCategory);
        return getDataTable(list);
    }

    /**
     * 导出存储商品类别的列表
     */
    @PreAuthorize("@ss.hasPermi('product:category:export')")
    @Log(title = "存储商品类别的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemCategory itemCategory)
    {
        List<ItemCategory> list = itemCategoryService.selectItemCategoryList(itemCategory);
        ExcelUtil<ItemCategory> util = new ExcelUtil<ItemCategory>(ItemCategory.class);
        util.exportExcel(response, list, "存储商品类别的数据");
    }

    /**
     * 获取存储商品类别的详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(itemCategoryService.selectItemCategoryById(id));
    }

    /**
     * 新增存储商品类别的
     */
    @PreAuthorize("@ss.hasPermi('product:category:add')")
    @Log(title = "存储商品类别的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemCategory itemCategory)
    {
        return toAjax(itemCategoryService.insertItemCategory(itemCategory));
    }

    /**
     * 修改存储商品类别的
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "存储商品类别的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemCategory itemCategory)
    {
        return toAjax(itemCategoryService.updateItemCategory(itemCategory));
    }

    /**
     * 删除存储商品类别的
     */
    @PreAuthorize("@ss.hasPermi('product:category:remove')")
    @Log(title = "存储商品类别的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itemCategoryService.deleteItemCategoryByIds(ids));
    }
}
